package com.movie.controller.SysVisualizationController;

import com.movie.beans.base.AjaxResult;
import com.movie.beans.dto.ChartPoint;
import com.movie.beans.dto.ChartRate;
import com.movie.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/visualization")
public class SysVisualizationController {

    private static String prefix = "/system/visualization";
    @Autowired
    private ChartService chartService;
    @GetMapping()
    public String index(){
        return prefix+"/index";
    }

    @PostMapping("/rate")
    @ResponseBody
    public AjaxResult rate(){
        List<Map> res = new ArrayList<>();
        List<ChartRate> list = chartService.selectChartRate();
        for (ChartRate chartRate:list) {
            Map map = new HashMap();
            map.put("value",chartRate.getCount());
            map.put("name",chartRate.getRate());
            res.add(map);
        }
        return AjaxResult.success(res);
    }

    @PostMapping("/point")
    @ResponseBody
    public AjaxResult point(){
        List<Integer> data = new ArrayList<>();
        List<String> label = new ArrayList<>();
        List<ChartPoint> list = chartService.selectChartPoint();
        for (ChartPoint point:list) {
            data.add(point.getCount());
            label.add(Double.toString(point.getPoint()));
        }
        Map map = new HashMap();
        map.put("data",data);
        map.put("label",label);
        return AjaxResult.success(map);
    }
}
